﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Website
{
    public partial class Gallery : System.Web.UI.Page
    {
        private readonly IList<ImageButton> _images = new List<ImageButton>();

        protected void Page_Load(object sender, EventArgs e)
        {
            string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Images"));
            var images = new List<string>(filesindirectory.Count());

            foreach(string item in filesindirectory)
            {
                images.Add(String.Format("~/Images/{0}", System.IO.Path.GetFileName(item)));
            }

            RepeaterImages.DataSource = images;
            RepeaterImages.DataBind();
            UploadImage();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string filename = FileUpload1.FileName;
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images" + filename));

               Response.Redirect("~/Gallery.aspx");

            }
        }

        private void UploadImage()
        {
            foreach (string strFilename in Directory.GetFiles(Server.MapPath("~/Images")))
            {
                ImageButton imageButton = new ImageButton();
                FileInfo fileinfo = new FileInfo(strFilename);
                imageButton.ImageUrl = "~/Images/" + fileinfo.Name;
                imageButton.Width = Unit.Pixel(100);
                imageButton.Height = Unit.Pixel(100);
                imageButton.Style.Add("padding", "5px");
                imageButton.Click += new ImageClickEventHandler(imageButton_Click);
                Panel1.Controls.Add(imageButton);
            }
        }

        void imageButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            Response.Redirect("~/Image.aspx?ImageURL=" + ((ImageButton)sender).ImageUrl);
        }     
    }
}